package TouristManagement.ResponsableSite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import TouristManagement.LocalStat;
import TouristManagement.ServerNamingTourist;
import TouristManagement.ServerNamingTouristHelper;
import TouristManagement.Site;
import TouristManagement.SiteHelper;


public class ResponsableSite {
	
	private short idSite;
	private Site leSite;
	private ServerNamingTourist leServerNamingTourist;
	
	public ResponsableSite(short idSite) {
		super();
		this.idSite = idSite;
	}
	
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void connectToSiteServer(){
		//connect to site server	
		try {			
			Properties props = new Properties();
	        props.put("org.omg.CORBA.ORBInitialPort", "2001");
	        props.put("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
	        
	        String args[] = null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
	        
	        //site1
	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.get_nomSites()[idSite-1],"");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantSite = nameRoot.resolve(nameToFind);
	        System.out.println("Objet site trouve aupres du service de noms. IOR de l'objet :");
	        System.out.println(orb.object_to_string(distantSite));
	        
	        leSite = SiteHelper.narrow(distantSite);
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}	
	
	public void afficherMenuAccueil(){
		int n;
		do {
			for (int i = 0; i < 10; i++) {		
			   System.out.println();
			  }
			System.out.println("******************************************");
			System.out.println("WELCOME TO TOURIST MANAGEMENT FOR SITE RESPONSIBLE");
			System.out.println("			MENU ");
			System.out.println("NOM SITE : "+ leSite.getNomSite());
			System.out.println("ID SITE : "+ this.idSite);
			System.out.println("1. Consult local statistique ");
			System.out.println("2. Quit ");
			System.out.print("Please enter your choice : ");
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	        String choix = null;
			try {
				choix = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        n = Integer.parseInt(choix); 
		}while (n!=1 && n!=2); 
		if (n==1) {
			LocalStat localStat = this.consultStatLocal();
			System.out.println("Nom site : " + leSite.getNomSite());
			System.out.println("Nombre visiteurs actuel : " + localStat.currentNbVisitors);
			System.out.println("Duree de visite moyenne : " + localStat.durationAverage);
			System.out.println("Duree de visite maximale : " + localStat.durationMax);
			System.out.println("Duree de visite minimale : " + localStat.durationMin);
		}
		else {
			//quit
			System.out.println("Closing program...");
			Thread.currentThread().interrupt();		
		}
	}
	
	public LocalStat consultStatLocal(){
		LocalStat localStat = leSite.consultLocalStat();
		return localStat;
	}
	
	public static void main (String args[]) {
		
		ResponsableSite leResponsableSite = new ResponsableSite((short)-1);
		leResponsableSite.connectToServerNamingTourist();	
		
		System.out.println("******************************************");
		System.out.println("WELCOME TO TOURIST MANAGEMENT FOR RESPONSALBE");		
		System.out.println("			MENU ");
		int noSite=1;
		for (String nomSite:leResponsableSite.leServerNamingTourist.get_nomSites()) {
			System.out.println(noSite+". "+nomSite);
			noSite++;
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String choix = null;
		int n=0;
		do {
			System.out.print("Please enter your site id : ");				
			try 
			{
				choix = in.readLine();
			    n = Integer.parseInt(choix); 
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		} while (n<0 || n>leResponsableSite.leServerNamingTourist.get_nomSites().length);
		leResponsableSite.idSite=(short) (n);
		//connect to objet corba site corresponding
		leResponsableSite.connectToSiteServer();	
		leResponsableSite.afficherMenuAccueil();		
	}
	

}
